#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class TianjiOnline_MDX2(Model3D):
    '''Replace 'TianjiOnline_MDX2' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(TianjiOnline_MDX2,self).__init__("MDX2")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_textures(self):
        
        numTex = self.inFile.read_long()
        prevTex = ""
        for i in range(numTex):
            texName = self.inFile.read_string(0x104, "\x00")
            if not texName:
                texName = prevTex
            self.create_material(i)
            self.add_material_name(i, "mat[%d]" %i)
            self.add_texture_name(i, texName)
            
            prevTex = texName
            
    def parse_materials(self, size):
        
        sectEnd = self.inFile.tell() + size
        while self.inFile.tell() != sectEnd:
            chunk = self.inFile.read_string(4)
            chunkSize = self.inFile.read_long()
            if chunk == "matl":
                pass
            elif chunk == "lays":
                self.inFile.seek(chunkSize, 1)
            elif chunk == "cras":
                pass
            elif chunk == "clra":
                pass
                self.inFile.seek(chunkSize, 1)
                
    def parse_vertices(self, meshName, numVerts):
        
        for i in range(numVerts):
            vx, vz, vy = self.inFile.read_float(3)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, -vz])
            
        for i in range(numVerts):
            nx, nz, ny = self.inFile.read_float(3)
            self.add_vert_normals(meshName, i, [nx, ny, -nz])
                
        for i in range(numVerts):
            tu = self.inFile.read_float()
            tv = self.inFile.read_float() 
            self.add_vert_uv(meshName, i, [tu, tv])
            
        for i in range(numVerts):
            bIndex = self.inFile.read_byte()
        
    def parse_faces(self, meshName, numFaces, matNum):
        
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, matNum)
            
    def parse_mesh(self, size):
        
        meshNum = 0        
        sectEnd = self.inFile.tell() + size
        while self.inFile.tell() != sectEnd:
            chunk = self.inFile.read_string(4)
            chunkSize = self.inFile.read_long()
            if chunk == "chks":
                meshName = "temp[%d]" %meshNum
                meshNum += 1
                self.create_object(meshName)
                numVerts = self.inFile.read_long()
                numFaces = self.inFile.read_long()
                matNum = self.inFile.read_long()
                print meshName, matNum
                self.parse_vertices(meshName, numVerts)
                self.parse_faces(meshName, numFaces, matNum)                
            elif chunk == "name":
                self.inFile.seek(chunkSize, 1)
            elif chunk == "gcpt":
                self.inFile.seek(chunkSize, 1)
            else:
                self.inFile.seek(chunkSize, 1)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        filesize = self.inFile.filesize()
        
        while self.inFile.tell() != filesize:
            chunk = self.inFile.read_string(4)
            chunkSize = self.inFile.read_long()
            if chunk == "vers":
                version = self.inFile.read_long()
            elif chunk == "texs":
                self.parse_textures()
            elif chunk == "mtls":
                self.parse_materials(chunkSize)
            elif chunk == "geom":
                print "geom"
                self.parse_mesh(chunkSize)
            else:
                self.inFile.seek(chunkSize, 1)

        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = TianjiOnline_MDX2(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "vers", "MDX2", "Tianji Online MDX2"

if __name__ == '__main__':
    
    file1 = "taoist_glove_male_11461.mdx2"
    file2 = "dqbfjianzhuditu_9189.mdx2"
    obj = read_file(file1)